//数值转为数字
function val2Number(val) {
    if (typeof val == 'boolean') {
      return val ? 1 : 0;
    } else if (typeof val == 'number') {
      return val;
    } else if (typeof val == 'string') {
      // val = val.match(/[0-9\.e]+/)[0];
      return parseFloat(val) ? val : 0;
    } else {
      return 0;
    }
  }
  //数值转字符串
  function val2String(val) {
    if (typeof val == 'boolean') {
      return val ? 'true' : 'false';
    } else if (typeof val == 'string') {
      return val;
    } else if (typeof val == 'number') {
      return val + '';
    } else {
      return '';
    }
  }
  //数值转布尔值
  function val2Boolean(val) {
    if (typeof val == 'boolean') {
      return val;
    } else if (typeof val == 'string') {
      return val ? true : false;
    } else if (typeof val == 'number') {
      return val > 0 ? true : false;
    } else {
      return false;
    }
  }
  //数值转日期
  function val2Date(val) {
      
    if (typeof val == 'string'&&val.indexOf('-')>0) {
      val = val.match(/[0-9\.a-zA-Z\-\/]+/)[0];
      return new Date(val).format('yyyy-MM-dd');
    } else if (typeof val == 'number') {
        if(val<978307200000){
            val=((val-70*365-19)*86400-8*3600)*1000;
        }
      return new Date(val).format('yyyy-MM-dd');
    }else if(typeof val == 'object'&&val){
      return new Date(val.time?val.time:0).format('yyyy-MM-dd');
    } else {
      return '';
    }
  }
  //数值转日期时间
  function val2Datetime(val) {
    if (typeof val == 'string') {
      val = val.match(/[0-9\.a-zA-Z\-\/\s\:]+/)[0];
      return new Date(val).format('yyyy-MM-dd hh:mm:ss');
    } else if (typeof val == 'number') {
        if(val<978307200000){
            val=((val-70*365-19)*86400-8*3600)*1000;
        }
      return new Date(val).format('yyyy-MM-dd hh:mm:ss');
    }else if(typeof val == 'object'&&val){
      return new Date(val.time?val.time:0).format('yyyy-MM-dd hh:mm:ss');
    }  else {
      return '';
    }
  }
  //改变数值类型
  function changeType(val, type) {
    switch (type) {
      case 'number':
        return val2Number(val);
      case 'string':
        return val2String(val);
      case 'boolean':
        return val2Boolean(val);
      case 'date':
        return val2Date(val);
      case 'datetime':
        return val2Datetime(val);
      default:
        return '';
    }
  }
  
  export default {
    val2Number,
    val2String,
    val2Boolean,
    changeType,
    val2Date,
    val2Datetime
  };
  